﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ServiceDKHP;
using ServiceSinhVien;
using ServiceLichDangKy;
using ServiceThongTinDangKy;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
		if (Page.IsPostBack) return;

		// lay thong tin sinh vien
		ServiceXuLyThongTinSinhVienSoapClient sinhVienSv = new ServiceXuLyThongTinSinhVienSoapClient();
		string tenDangNhap = Page.User.Identity.Name;
		int maSinhVien = int.Parse(tenDangNhap);
		ServiceSinhVien.SINHVIEN sv = sinhVienSv.layThongTinSinhVien(maSinhVien);
		spanHoTen.InnerText = sv.HOTEN;

		// lay lich dang ky
		ServiceXuLyThongTinLichDangKySoapClient ldkSv = new ServiceXuLyThongTinLichDangKySoapClient();
		ServiceLichDangKy.LICHDANGKY ldk = ldkSv.layLichDangKyHienTai();
		lbNamHoc.InnerText = ldk.NAMHOC.ToString();
		lbHocKy.InnerText = ldk.HOCKY.ToString();
		lbMaxTC.InnerText = ldk.TINCHITOIDA.ToString();
		lbMinTC.InnerText = ldk.TINCHITOITHIEU.ToString();

		// lay danh sach lop sinh vien da dk trong hk nay
		ServiceXuLyThongTinDangKySoapClient ttdkSv = new ServiceXuLyThongTinDangKySoapClient();
		GridView1.DataSource = ttdkSv.layDanhSachLopDaDangKy(maSinhVien);
		GridView1.DataBind();

		// lay danh sach lop con lai duoc phep dk trong hk nay
		GridView2.DataSource = ttdkSv.layDanhSachLopCoTheDangKy(maSinhVien);
		GridView2.DataBind();

	}

	protected void btnCancel_Command(object sender, CommandEventArgs e)
	{
		int maLop = int.Parse(e.CommandArgument.ToString());
		int maSinhVien = int.Parse(Page.User.Identity.Name);
		ServiceDangKyHocPhanSoapClient dkhpSv = new ServiceDangKyHocPhanSoapClient();
		if (dkhpSv.huyDangKy(maSinhVien, maLop))
			Response.Redirect("Default.aspx");
	}

	protected void btnRegister_Command(object sender, CommandEventArgs e)
	{
		int maLop = int.Parse(e.CommandArgument.ToString());
		int maSinhVien = int.Parse(Page.User.Identity.Name);
		ServiceDangKyHocPhanSoapClient dkhpSv = new ServiceDangKyHocPhanSoapClient();
		if (dkhpSv.dangKyHocPhan(maSinhVien, maLop))
			Response.Redirect("Default.aspx");
		else
			Response.Write("<script>alert('Đăng ký thất bại!');window.location = 'Default.aspx';</script>");
	}
}